package com.huobao.yunying.adapters;

import java.util.List;

import com.huobao.yunying.entity.TemaiDetail_New;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.zhangying.R;
import com.tonicartos.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class Adapter_Temai_Detail_New extends BaseAdapter implements StickyGridHeadersSimpleAdapter  {
	private Context context;
	private LayoutInflater inflater;
	private List<TemaiDetail_New> details;

	public Adapter_Temai_Detail_New(Context context,List<TemaiDetail_New> details) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.details = details;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return details.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return details.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_temai_detail_grid, null);
		}
		final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
		TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
		TextView qianggou = (TextView) convertView.findViewById(R.id.btn_1);
		TemaiDetail_New arg1 = details.get(position);
		ImageUtils.loadimg(imageView, arg1.getDefault_image(), new OnImageLoadListener() {

			@Override
			public void onLoadSuccess(Drawable drawable) {
				int newWidth = ScreenUtil.getInstant().getDisWidth(context) / 2 - 20;
				int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
				LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
				layoutParams.width = newWidth;
				layoutParams.height = newHight;
				imageView.setLayoutParams(layoutParams);
			}
		});
		if (arg1.getGenre().equals("1") || arg1.getGenre().equals("2"))
			tv_title.setText("￥" + arg1.getPrice());
		else if (arg1.getGenre().equals("3"))
			tv_title.setText(arg1.getSzhaoshang());
		qianggou.setText(arg1.getStext());
		return convertView;
	}

	@Override
	public long getHeaderId(int position) {
		// TODO Auto-generated method stub
		return Long.parseLong(details.get(position).getRecom_id());
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_temai_detail_new, null);
		}
		TemaiDetail_New data = details.get(position);
		final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
		ImageUtils.loadimg(imageView, data.getRecom_logo(), new OnImageLoadListener() {
			
			@Override
			public void onLoadSuccess(Drawable drawable) {
				int newWidth = ScreenUtil.getInstant().getDisWidth(context);
				int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
				LayoutParams layoutParams = new LayoutParams(newWidth, newHight);
				imageView.setLayoutParams(layoutParams);
			}
		});
		return convertView;
	}

}